package com.itheima.controller.order;

import com.itheima.api.order.OmsOrderService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.dto.OrderDto;
import com.itheima.vo.pojo.OmsOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OmsOrderController {

    @DubboReference
    private OmsOrderService orderService;

    /**
     * 分页查询订单
     *
     * @param pageNum
     * @param pageSize
     * @param orderSn
     * @param receiverKeyword
     * @param status
     * @param orderType
     * @param sourceType
     * @param createTime
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Integer pageNum, Integer pageSize, String orderSn, String receiverKeyword,
                           Integer status, Integer orderType, Integer sourceType, String createTime) {
        Map list = orderService.list(pageNum, pageSize, orderSn, receiverKeyword,
                status, orderType, sourceType, createTime);
        return AjaxResult.success(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Integer> ids) {
        log.info("ids ={}", ids);
        orderService.delete(ids);
        return AjaxResult.success();
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     * @return
     */
    @PostMapping("/update/close")
    public AjaxResult update(@RequestParam  List<Integer> ids,String note) {
        orderService.updateStatus(ids,note);
        return AjaxResult.success();
    }

    /**
     * 批量发货
     *
     * @param list
     * @return
     */
    @PostMapping("/update/delivery")
    public AjaxResult delivery(@RequestBody List<OmsOrder> list) {
        log.info("list ={}", list);
        orderService.delivery(list);
        return AjaxResult.success();
    }

    /**
     * 修改订单费用信息
     *
     * @return
     */
    @PostMapping("/update/moneyInfo")
    public AjaxResult moneyInfo(@RequestBody OmsOrder order) {
        orderService.moneyInfo(order);
        return AjaxResult.success();
    }

    /**
     * 备注订单
     *
     * @param order
     * @return
     */
    @PostMapping("/update/note")
    public AjaxResult note(OmsOrder order) {
        orderService.note(order);
        return AjaxResult.success();
    }

    /**
     * 修改收货人信息
     *
     * @param order
     * @return
     */
    @PostMapping("/update/receiverInfo")
    public AjaxResult receiverInfo(@RequestBody OmsOrder order) {
        orderService.receiverInfo(order);
        return AjaxResult.success();
    }

    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getOne(@PathVariable Long id) {
        OrderDto orderDto = orderService.findById(id);
        return AjaxResult.success(orderDto);
    }
}
